package cn.enjoy.rabbitmq.controller;

import cn.enjoy.rabbitmq.config.MqQueueConfig;
import cn.enjoy.tool.api.R;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author K
 * @date 2020/12/4 11:16
 */
@RestController
public class RabbitMqController {
    @Autowired
    RabbitTemplate rabbitTemplate;


    @PostMapping("/sentWorkQueue")
    public R sentWorkQueue(String messageData) {
        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend(MqQueueConfig.QUEUE_WORK, map);

        return R.success("OK");
    }

    @PostMapping("/sentFanout")
    public R sentFanout(String messageData) {
        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("fanout.mode", "", map);

        return R.success("OK");
    }

    @PostMapping("/sentDirectToId1")
    public R sentDirectToId1(String messageData) {
        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("direct.mode", "id.1", map);

        return R.success("OK");
    }

    @PostMapping("/sentDirectToId2")
    public R sentDirectToId2(String messageData) {
        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("direct.mode", "id.2", map);

        return R.success("OK");
    }

    @PostMapping("/sentTTL")
    public R sentTTL(String messageData, int count) {
        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("createTime", createTime);

        for (int i = 1; i <= count; i++) {
            map.put("messageData", messageData + i);
            //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
            rabbitTemplate.convertAndSend("ttl.queue", map);
        }

        return R.success("OK");
    }

    @PostMapping("/sentDelay")
    public R sentDelay(String messageData, int count, long seconds) {
        Map<String, Object> map = new HashMap<>();

        for (int i = 1; i <= count; i++) {
            String messageId = String.valueOf(UUID.randomUUID());
            String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            map.put("messageId", messageId);
            map.put("messageData", messageData + i);
            map.put("createTime", createTime);

            rabbitTemplate.convertAndSend("delay.mode", "delay.notice", map, new MessagePostProcessor() {
                @Override
                public Message postProcessMessage(Message message) throws AmqpException {
                    //给消息加上header,表明是一个延迟消息，并且设置过期毫秒数
                    message.getMessageProperties().setHeader("x-delay", seconds * 1000);
                    return message;
                }
            });
        }

        return R.success("OK");
    }

    @PostMapping("/sentManual")
    public R sentManual(String messageData, int count) {
        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);

        map.put("createTime", createTime);
        for (int i = 1; i <= count; i++) {
            map.put("messageData", messageData + i);
            //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
            rabbitTemplate.convertAndSend("manualAck.queue", map);
        }

        return R.success("OK");
    }

    @PostMapping("/sentNormalToDLX")
    public R sentNormalToDLX(String messageData) throws JsonProcessingException {
        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("createTime", createTime);
        map.put("messageData", messageData);

        ObjectMapper objectMapper = new ObjectMapper();
        rabbitTemplate.convertAndSend("NORMAL_EXCHANGE", "normal.dlx.one", map);
//        rabbitTemplate.convertAndSend("NORMAL_EXCHANGE", "normal.dlx.api", messageData);

        return R.success("OK");
    }

}
